package com.hsh.db;

import java.io.File;
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import com.hsh.CommonUtils;

public class WordCount extends Mapper<LongWritable, MySqlDBWriteAble, Text, NullWritable>{
	
	private NullWritable nullValue = NullWritable.get();
	
	@Override
	protected void map(LongWritable key, MySqlDBWriteAble value, Context context) 
			throws java.io.IOException ,InterruptedException {
		
		Text output = new Text();
		output.set(value.toString());
		System.out.println(output);
		context.write(output, nullValue);
	}
	
	@Override
	public void run(Context context) 
			throws java.io.IOException ,InterruptedException {
		while (context.nextKeyValue()) {
			map(context.getCurrentKey(), context.getCurrentValue(), context);
		}
	}
	
	public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
	//	Path input = new Path("F:/MyEclipse Workspaces/EclipseWorkSpace/LocalMR/Input");
		File outPutDir = new File("F:/MyEclipse Workspaces/EclipseWorkSpace/LocalMR/Output");
		CommonUtils.deleteDir(outPutDir);
		if(outPutDir.exists())
			outPutDir.delete();
		Path output = new Path(outPutDir.toString());
		Configuration conf = new Configuration();
		conf.setStrings("hadoop.tmp.dir", "d://tmp");
		conf.set("tableName", "score");
		conf.set("columns", "stuNum,scor");
		

		Job job = new Job(conf, "word count0");
		job.setJarByClass(WordCount.class);
		job.setMapperClass(WordCount.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(NullWritable.class);
		job.setInputFormatClass(MySqlInputFormat.class);
		FileOutputFormat.setOutputPath(job, output);

		long begTime = System.currentTimeMillis();
		boolean flag = job.waitForCompletion(false);
		long endTime = System.currentTimeMillis();
		System.out.println("---耗时---" + (endTime - begTime)/1000);
		if(flag)
			System.exit(1);
		System.exit(0);
	}
}
